﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace FzWeb.Models
{
    /// <summary>
    /// 消息服务类
    /// </summary>
    public class MessageService
    {
        public Entities db
        {
            get
            {
                if (HttpContext.Current.Session["db"] == null)
                {
                    HttpContext.Current.Session["db"] = new Entities();
                }
                return HttpContext.Current.Session["db"] as FzWeb.Models.Entities;
            }
        }


        /// <summary>
        /// 获得特定用户的消息
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="type">0 消息,1 面试通知</param>
        /// <returns></returns>
        public IList<Message> GetMessages(int userid,int type)
        {
            return db.Message.Where(c => c.To == userid && c.Type==type).ToList();
        }

        /// <summary>
        /// 获得特定用户的消息
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        public IList<Message> GetMessages(int userid)
        {
            return db.Message.Where(c => c.To == userid).ToList();
        }

        /// <summary>
        /// 获得类型消息
        /// </summary>
        /// <param name="type">0 消息,1 面试通知</param>
        /// <param name="status">0 未读,1 以读</param>
        /// <returns></returns>
        public IList<Message> GetMessages(int userid,int type,int status)
        {
            return db.Message.Where(c => c.Type == type && c.To==userid && c.Status==status).ToList();
        }

        /// <summary>
        /// 获得未读消息
        /// </summary>
        /// <param name="type">0 消息,1 面试通知</param>
        /// <returns></returns>
        public IList<Message> GetUnreadMessages(int userid,int type)
        {
            return GetMessages(userid,type, 0);
        }

        public IList<Message> GetMessages()
        {
            return db.Message.ToList();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Message GetMessage(int id)
        {
            return db.Message.First(c => c.MessageId == id);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="m"></param>
        public void UpdateMessage(Message m)
        {
            db.ApplyPropertyChanges(GetMessage(m.MessageId).EntityKey.EntitySetName, m);
            db.SaveChanges();
        }

        public void DelMessage(int id)
        {
            db.DeleteObject(GetMessage(id));
            db.SaveChanges();
        }

        public void CreateMessage(Message msg)
        {
            db.AddToMessage(msg);
            db.SaveChanges();
        }


        internal void SaveChanges()
        {
            db.SaveChanges();
        }
    }
}
